<template>
  <div class="site-containner">
    <div class="app-container">
      <el-row>
        <el-col :span="24">
          <div class="outMore">
            <div class="dataTitle">
              完成配件更换
              <div class="returnIndex"><router-link :to="{path:'/monitor/system/components/records'}"><i class="el-icon-arrow-left titleIcon"></i>返回</router-link></div>
            </div>
          </div>
          <div>
            <div class="finishBody">
              <el-form :inline="true" class="addMessage" ref="form" :model="finishReplaceBody" label-width="90px">
                <el-form-item label="记录编号:">
                  <el-input class="readonlyMessage smallMessage" v-model="finishReplaceBody.data.record_id" readonly></el-input>
                </el-form-item>
                <el-form-item class="rightMessage" label="创建人:">
                  <el-input class="readonlyMessage smallMessage" v-model="finishReplaceBody.data.creator_user_name" readonly></el-input>
                </el-form-item>
                <el-form-item label="状态:">
                  <el-input class="readonlyMessage smallMessage" v-model="replaceStatus" readonly></el-input>
                </el-form-item>
                <el-form-item class="rightMessage" label="创建时间:">
                  <el-input class="readonlyMessage smallMessage" v-model="finishReplaceBody.data.creator_time" readonly></el-input>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 8px; color: red;">*</span>
                <el-form-item label="出厂编号:">
                  <el-select class="smallMessage" @clear="clearEquip" @change="getEquipName" v-model="finishReplaceBody.data.equip_cid" clearable filterable placeholder="请选择">
                    <el-option
                      v-for="item in equipSelect"
                      :key="item.equip_cid"
                      :label="item.equip_cid"
                      :value="item.equip_cid">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item class="rightMessage" label="设备名称:">
                  <el-input class="smallMessage readonlyMessageTwo" v-model="finishReplaceBody.data.equip_name" placeholder="选择设备编码自动获取" readonly></el-input>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 35px; color: red;">*</span>
                <el-form-item style="width: 100%" label="机构:">
                  <el-select class="smallMessage" @change="getFittingType" v-model="finishReplaceBody.data.fittings_type" clearable filterable placeholder="请选择">
                    <el-option
                      v-for="item in fittingsType"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 8px; color: red;">*</span>
                <el-form-item label="配件编码:">
                  <el-select class="smallMessage" @clear="clearOid" @change="getOidName" v-model="finishReplaceBody.data.fittings_id" clearable filterable placeholder="请选择">
                    <el-option
                      v-for="item in oidSelect"
                      :key="item.oid"
                      :label="item.oid"
                      :value="item.oid">
                    </el-option>
                  </el-select>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 22px; color: red;">*</span>
                <el-form-item class="rightMessage" label="配件名称:">
                  <el-select class="smallMessage" @clear="clearName" @change="getOidNumber" v-model="finishReplaceBody.data.fittings_name" clearable filterable placeholder="请选择">
                    <el-option
                      v-for="item in oidSelect"
                      :key="item.name"
                      :label="item.name"
                      :value="item.name">
                    </el-option>
                  </el-select>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 8px; color: red;">*</span>
                <el-form-item label="更换原因:">
                  <el-input class="cityMessage" v-model="finishReplaceBody.data.update_reason"></el-input>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px -5px; color: red;">*</span>
                <el-form-item label="新配件品牌:">
                  <el-input class="smallMessage" v-model="finishReplaceBody.data.new_fittings_brand"></el-input>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 8px; color: red;">*</span>
                <el-form-item class="rightMessage" label="新配件编号:">
                  <el-input class="smallMessage" v-model="finishReplaceBody.data.new_fittings_model"></el-input>
                </el-form-item>
                <el-form-item label="序列号:">
                  <el-input class="cityMessage" v-model="finishReplaceBody.data.new_fittings_num"></el-input>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 20px; color: red;">*</span>
                <el-form-item label="处理人:">
                  <el-select class="smallMessage" v-model="finishReplaceBody.data.handle_user_id" clearable filterable placeholder="请选择">
                    <el-option
                      v-for="item in peoples"
                      :key="item.account"
                      :label="item.name"
                      :value="item.account">
                    </el-option>
                  </el-select>
                </el-form-item>
                <span style="position: absolute; margin: 15px 0px 0px 20px; color: red;">*</span>
                <el-form-item class="rightMessage" label="完成时间:">
                  <el-date-picker
                    v-model="finishReplaceBody.data.finish_time"
                    type="datetime"
                    class="smallMessage"
                    format="yyyy-MM-dd HH:mm:ss"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期时间">
                  </el-date-picker>
                </el-form-item>
                <el-form-item class="pictureSize" label="附件:">
                  <img v-if="picShow" style="max-height: 320px; max-width: 630px" :src="picShow">
                  <el-upload
                    class="upload-demo"
                    action=""
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :http-request="uploadImg"
                    multiple
                    :limit="1"
                    list-type="picture"
                    :on-exceed="handleExceed">
                    <el-button style="margin-bottom: 10px; color: #52C2D2;" size="small">点击上传</el-button>
                    <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
                  </el-upload>
                </el-form-item>
              </el-form>
            </div>
            <el-button type="primary" class="clear"><router-link :to="{path:'/monitor/system/components/records'}">取消</router-link></el-button>
            <el-button class="indieWidth" type="primary" @click="finishReplaceWork">保存</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
  import axios from 'axios'
  export default {
    name: 'AdminSite',
    components: {
    },
    data() {
      return {
        picShow: '',
        replaceStatus: '',
        fittingsType: [],
        equipSelect: [],
        peoples: [],
        oidSelect: [{
          oid: '请先选择机构',
          name: '请先选择机构'
        }],
        getReplaceData: {
          access_token: localStorage.getItem('access_token'),
          type: 'update_log_get_by_id',
          id: this.$route.query.id
        },
        getFittingsType: {
          access_token: localStorage.getItem('access_token'),
          type: 'fittings_type_getall'
        },
        getEquipIdBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'equip_get_condition',
          data: {
            condition: {
              cid: '',
              cids: [],
              cid_filter: [],
              name: '',
              brand: '',
              model: '',
              equip_class: '',
              annual_trial_remind: 0,
              type: ''
            },
            page: {
              page_size: '100',
              page_number: '1'
            }
          }
        },
        getOidBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'oid_get_condition',
          data: {
            condition: {
              oid: '',
              oids: [],
              oid_filter: [],
              name: '',
              search: '',
              etl: '',
              fittings: '',
              fittings_type: '11',
              type: '',
              types: []
            },
            page: {
              page_size: '100',
              page_number: '1'
            }
          }
        },
        finishReplaceBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'update_log_finish',
          data: {
            record_id: '',
            status: 0,
            creater_user_id: '',
            creater_user_name: '',
            creater_time: '',
            equip_cid: '',
            equip_name: '',
            fittings_id: '',
            fittings_name: '',
            fittings_type: '',
            update_reason: '',
            new_fittings_brand: '',
            new_fittings_model: '',
            new_fittings_num: '',
            handle_user_id: '110',
            handle_user_name: '',
            finish_time: '',
            attachment: []
          }
        },
        // 获取人员信息
        getAccountsBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'get_accounts',
          data: ''
        }
      }
    },
    created() {
      this.getReplace()
      this.getType()
      this.getEquipId()
      this.getAccounts()
    },
    methods: {
      getFittingType() {
        this.finishReplaceBody.data.fittings_id = ''
        this.finishReplaceBody.data.fittings_name = ''
        this.getOidBody.data.condition.fittings_type = this.finishReplaceBody.data.fittings_type
        this.getOid()
      },
      getAccounts() {
        let httpStr = process.env.SERVER_URL_4A
        axios.post(httpStr + '/account', this.getAccountsBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.peoples = res.data.datas
        })
          .catch((error) => {
            console.log(error)
          })
      },
      getReplace() {
        let pic = process.env.BASE_API
        axios.post('/fittings', this.getReplaceData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.finishReplaceBody.data = res.data.data
          if (this.finishReplaceBody.data.status === 0) {
            this.replaceStatus = '未完成'
          } else {
            this.replaceStatus = '已完成'
          }
          if (this.finishReplaceBody.data.attachment.length > 0) {
            this.picShow = pic + '/' + this.finishReplaceBody.data.attachment[0].path
          }
          this.getOidBody.data.condition.fittings_type = this.finishReplaceBody.data.fittings_type
          this.getOid()
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      getEquipName() {
        for (let a = 0; a < this.equipSelect.length; a++) {
          if (this.equipSelect[a].equip_cid === this.finishReplaceBody.data.equip_cid) {
            this.finishReplaceBody.data.equip_name = this.equipSelect[a].name_label
          }
        }
      },
      getOidName() {
        for (let a = 0; a < this.oidSelect.length; a++) {
          if (this.oidSelect[a].oid === this.finishReplaceBody.data.fittings_id) {
            this.finishReplaceBody.data.fittings_name = this.oidSelect[a].name
          }
        }
      },
      getOidNumber() {
        for (let a = 0; a < this.oidSelect.length; a++) {
          if (this.oidSelect[a].name === this.finishReplaceBody.data.fittings_name) {
            this.finishReplaceBody.data.fittings_id = this.oidSelect[a].oid
          }
        }
      },
      clearEquip() {
        this.finishReplaceBody.data.equip_name = ''
      },
      clearOid() {
        this.finishReplaceBody.data.fittings_name = ''
      },
      clearName() {
        this.finishReplaceBody.data.fittings_id = ''
      },
      getEquipId() {
        axios.post('/equipmanage', this.getEquipIdBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.equipSelect = res.data.data.items
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      getType() {
        axios.post('/basicdata', this.getFittingsType, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.fittingsType = res.data.datas
        })
          .catch((error) => {
            console.log(error)
          })
      },
      getOid() {
        axios.post('/basicdata', this.getOidBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.oidSelect = res.data.data.items
        })
          .catch((error) => {
            console.log(error)
          })
      },
      handleRemove(file, fileList) {
        this.addReplaceBody.data.attachment = []
      },
      handlePreview(file) {
        console.log(file)
      },
      handleExceed(files, fileList) {
        // this.$message.warning(`上传失败，当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
        this.$message.error(`上传失败，当前限制选择 1 个文件。`)
      },
      uploadImg(item) {
        const formData = new FormData()
        formData.append('file', item.file)
        // formData.append('group', 'system')
        axios.post('/equipmanage/upload', formData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
          .then((res) => {
            this.finishReplaceBody.data.attachment = res.data.datas
            this.picShow = ''
          })
          .catch((err) => {
            console.log(err, 'error')
          })
      },
      finishReplaceWork() {
        for (var i = 0; i < this.peoples.length; i++) {
          if (this.peoples[i].account === this.finishReplaceBody.data.handle_user_id) {
            this.finishReplaceBody.data.handle_user_name = this.peoples[i].name
          }
        }
        axios.post('/fittings', this.finishReplaceBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.$router.push({
              path: '/monitor/system/components/records'
            })
          }
        })
          .catch((error) => {
            this.$message.error(error.data.errmsg)
          })
      }
    }
  }
</script>
<style scoped>
  .site-containner{
    font-size: 16px;
    color: #0072F3;
  }
  .outMore{
    height: 40px;
    padding-top: 2px;
    border-bottom: 1px solid #E0E0E0;
  }
  .dataTitle {
    padding-left: 5px;
    height: 16px;
    font-size: 16px;
    color: #323842;
  }
  .returnIndex {
    float: right;
    font-size: 14px;
    color: #0072F3;
    cursor: pointer;
  }
  .finishBody{
    width: 100%;
    min-height: 500px;
    margin-top: 20px;
    padding: 0px 20px;
    border: 1px solid #E0E0E0;
    background-color: white;
  }
  /deep/ .textTitle {
    color: #323842;
    font-size: 14px;
    font-weight: bold;
    padding:20px 0px;
    border-bottom: 1px solid #E0E0E0;
  }
  /deep/ .addMessage {
    margin: 20px 0px;
    width: 735px;
  }
  /deep/ .el-form-item {
    margin-bottom: 10px;
    height: 30px;
  }
  /deep/ .el-input__inner {
    height: 30px;
  }
  /deep/ .smallMessage {
    width: 256px;
  }
  /deep/ .cityMessage {
    width: 631px;
  }
  /deep/ .coordinateMessage {
    width: 567px;
  }
  /deep/ .export {
    background: #fff;
    height: 30px;
    padding: 0px 15px;
    border: 1px solid #0072F3;
    color: #0072F3;
  }
  /deep/ .rightMessage {
    margin-left: 15px;
  }
  /deep/ .readonlyMessage > input{
    border:none;
  }
  /deep/ .readonlyMessageTwo > input {
    background: #EFEFEF;
  }
  /deep/ .el-icon-circle-close-outline:before {
    color: #D05133;
    font-size: 20px;
    font-weight: bold;
  }
  /deep/ .clear{
    padding: 6px 16px;
    background-color: #758aa1;
    border-color: #758aa1 ;
    color: #fff;
    float: right;
    margin-top: 20px;
    margin-left: 10px;
  }
  /deep/ .indieWidth {
    padding: 6px 16px;
    float: right;
    border:1px solid #0072F3;
    background: #0072F3;
    margin-top: 20px;
  }
  /deep/ .relation {
    float: right;
    height: 30px;
    font-size: 14px;
    padding: 6px 16px;
    background: #0072F3;
    border-color: #0072F3;
  }
  /deep/ .moreLong > .el-form-item__content > .el-select > .el-input > input{
    width: 256px
  }
  /deep/ .el-table--border {
    margin-top: 20px;
  }
  /deep/ .el-table__header-wrapper {
    height: 40px;
  }
  /deep/ .el-table--border td, .el-table--border th, .el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed {
    border-right: 1px solid #E3E3E3;
    padding: 4px;
  }
  /deep/ .el-table--border th {
    padding: 3px;
  }
  /deep/ .el-table--striped .el-table__body tr.el-table__row--striped td {
    background: #F0F2F7;
  }
  /deep/ .pictureSize {
    height: auto;
  }
</style>
